package com.xiweicheng.test.proxy.doc.handler;

import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.xiweicheng.test.proxy.Utils;

public class ImgDocHandler implements IDocHandler {

	@Override
	public void handle(Document doc, String baseUrl, String basePath) {
		Elements imgs = doc.select("img");
		imgs.forEach(img -> {
			String src = img.attr("src");
			if (!src.matches("^(\\s*http[s]?://).*")) {
				if (!src.startsWith("/")) {
					img.attr("src", Utils.joinPaths(Utils.getBasePath(basePath), src.trim()));
				}
			}
		});
	}

}
